﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Utilities;

namespace BusinessLogicLayer
{
    public class DuAnManager
    {
        public ActionResultAsList<DuAn> LayDSDA()
        {
            var ar = new ActionResultAsList<DuAn>();
            try
            {
                using (var db = new QLNVDbContext())
                {
                    ar.ObjectList = db.DuAns.ToList();
                }
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                ar.AddException(ex);
            }
            return ar;
        }

        public ActionResult ThemDA(DuAn da)
        {
            var ar = new ActionResult();
            try
            {
                using (var db = new QLNVDbContext())
                {

                    if (da.NgayBD.CompareTo(da.NgayKT) < 0)
                    {
                        db.DuAns.Add(da);
                        db.SaveChanges();
                    }
                    else
                    {
                        ar.AddException(new Exception("Lỗi!!! Ngày bắt đầu lớn hơn ngày kết thúc"));
                    }
                }
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                ar.AddException(ex);
            }
            return ar;
        }

        public ActionResult SuaDA(DuAn da)
        {
            var ar = new ActionResult();
            try
            {
                using (var db = new QLNVDbContext())
                {
                    var duan = db.DuAns.Single(s => s.MaDuAn == da.MaDuAn);
                    if (da.NgayBD.CompareTo(da.NgayKT) < 0)
                    {
                        duan.TenDuAn = da.TenDuAn;
                        duan.NgayBD = da.NgayBD;
                        duan.NgayKT = da.NgayKT;
                        duan.MoTa = da.MoTa;
                        db.SaveChanges();
                    }
                    else
                    {
                        ar.AddException(new Exception("Lỗi!!! Ngày bắt đầu lớn hơn ngày kết thúc"));
                    }
                }
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                ar.AddException(ex);
            }
            return ar;
        }

        public List<NhanVien> dsnv = new List<NhanVien>();

        public ActionResult XoaDA(string maDA)
        {
            var ar = new ActionResult();
            try
            {
                using (var db = new QLNVDbContext())
                {
                    var duan = db.DuAns.Single(s => s.MaDuAn == maDA);
                    List<NhanVien> temp = duan.NhanViens.ToList();
                    foreach (var item in duan.NhanViens.ToList())
                    {
                        duan.NhanViens.Remove(item);
                    }
                    db.DuAns.Remove(duan);
                    db.SaveChanges();
                    foreach (var nv in temp)
                    {
                        CapNhatTrangThaiNV(nv);
                    }
                    db.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                ar.AddException(ex);
            }
            return ar;
        }

        public void CapNhatTrangThaiNV(NhanVien nv)
        {
            using (var db = new QLNVDbContext())
            {
                List<DuAn> duan = db.DuAns.ToList();
                if (duan.Count == 0)
                {
                    nv.TrangThai = "Rảnh rỗi";
                }
                else
                {
                    foreach (var item in duan)
                    {
                        var da = db.DuAns.Include("NhanViens").Single(d => d.MaDuAn == item.MaDuAn);
                        if (da.NhanViens.ToList().Count == 0)
                        {
                            nv.TrangThai = "Rảnh rỗi";
                        }
                        else
                        {
                            foreach (var nhanvien in da.NhanViens.ToList())
                            {
                                if (nhanvien.MaNhanVien == nv.MaNhanVien)
                                {
                                    nv.TrangThai = "Làm việc";
                                    break;
                                }
                                else
                                {
                                    nv.TrangThai = "Rảnh rỗi";
                                }
                            }
                        }
                        if (nv.TrangThai == "Làm việc")
                            break;
                    }
                }
                db.SaveChanges();
            }
        }

        public ActionResultAsList<DuAn> TKDATheoTen(string tenDA)
        {
            var ar = new ActionResultAsList<DuAn>();
            try
            {
                using (var db = new QLNVDbContext())
                {
                    var list = db.DuAns.Where(s => s.TenDuAn.Contains(tenDA));
                    ar.ObjectList = list.ToList();
                }
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                ar.AddException(ex);
            }
            return ar;
        }

        public ActionResultAsList<DuAn> TKDATheoThang(int thang, int nam)
        {
            var ar = new ActionResultAsList<DuAn>();
            try
            {
                using (var db = new QLNVDbContext())
                {
                    var list = db.DuAns.Where(s => s.NgayBD.Month == thang && s.NgayBD.Year == nam);
                    ar.ObjectList = list.ToList();
                }
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                ar.AddException(ex);
            }
            return ar;
        }

        public ActionResultAsList<DuAn> TKDAChuaHetHan()
        {
            var ar = new ActionResultAsList<DuAn>();
            try
            {
                using (var db = new QLNVDbContext())
                {
                    var list = db.DuAns.Where(s => s.NgayKT.CompareTo(DateTime.Now) > 0).ToList();
                    ar.ObjectList = list.ToList();
                }
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                ar.AddException(ex);
            }
            return ar;
        }

        public ActionResultAsList<NhanVien> LayDSNVTrongDA(string maDA)
        {
            var ar = new ActionResultAsList<NhanVien>();
            try
            {
                using (var db = new QLNVDbContext())
                {
                    var da = db.DuAns.Include("NhanViens").Single(d => d.MaDuAn == maDA);
                    ar.ObjectList = da.NhanViens.ToList();
                }
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                ar.AddException(ex);
            }
            return ar;
        }

        public ActionResultAsList<NhanVien> LayDSChiTietNV(string maDA)
        {
            var ar = new ActionResultAsList<NhanVien>();
            try
            {
                using (var db = new QLNVDbContext())
                {
                    var da = db.NhanViens.Include("ChucVu1").Include("NhanVien2").Where(s => s.DuAns.Any(d => d.MaDuAn == maDA));
                    ar.ObjectList = da.ToList();
                }
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                ar.AddException(ex);
            }
            return ar;
        }

        public ActionResultAsList<NhanVien> LayDSNVThemDA()
        {
            var ar = new ActionResultAsList<NhanVien>();
            try
            {
                using (var db = new QLNVDbContext())
                {
                    var list = db.NhanViens.Include("ChucVu1").Include("NhanVien2").Where(s => s.TrangThai != "On-Site" && s.TrangThai != "Nghỉ việc");
                    ar.ObjectList = list.ToList();
                }
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                ar.AddException(ex);
            }
            return ar;
        }

        public ActionResult DangKyDA(string maDA, List<NhanVien> dsNV)
        {
            dsnv = dsNV;
            ActionResult ac = new ActionResult();
            try
            {
                using (var db = new QLNVDbContext())
                {
                    DuAn da = db.DuAns.Include("NhanViens").Single(n => n.MaDuAn == maDA);
                    da.NhanViens.Clear();
                    foreach (NhanVien ma in dsNV)
                    {
                        NhanVien nv = db.NhanViens.Single(k => k.MaNhanVien == ma.MaNhanVien);
                        nv.TrangThai = "Làm việc";
                        da.NhanViens.Add(nv);
                    }
                    db.SaveChanges();
                    List<NhanVien> dsnhanvien = db.NhanViens.ToList();
                    foreach (NhanVien nv in dsnhanvien)
                    {
                        CapNhatTrangThaiNV(nv);
                    }
                    db.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                ac.AddException(ex);
            }
            return ac;
        }

        public ActionResultAsList<NhanVien> LayDSNVTheoMaDA(string maDA)
        {
            var ar = new ActionResultAsList<NhanVien>();
            try
            {
                using (var db = new QLNVDbContext())
                {
                    ar.ObjectList = db.DuAns.Include("NhanViens").Single(n => n.MaDuAn == maDA).NhanViens.ToList();
                }
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                ar.AddException(ex);
            }
            return ar;
        }
    }
}